package tcu.ead2021.watermanage.dao;

import org.springframework.stereotype.Repository;
import tcu.ead2021.watermanage.entity.Drink;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Repository
public class DrinkDaoImpl implements DrinkDao {
    private final List<Drink> drinkList;

    public DrinkDaoImpl() {
        this.drinkList = new ArrayList<>();
    }

    @Override
    public List<Drink> selectAll() {
        return this.drinkList;
    }

    @Override
    public Drink selectById(String id) {
        return drinkList.stream()
                .filter(drink -> Objects.equals(id, drink.getId()))
                .findFirst().orElse(null);
    }

    @Override
    public void insert(Drink drink) {
        this.drinkList.add(drink);
    }

    @Override
    public void updateById(Drink drink) {
        this.drinkList.replaceAll(drinkTemp -> Objects.equals(drink.getId(), drinkTemp.getId()) ? drink : drinkTemp);
    }

    @Override
    public void deleteById(String id) {
        this.drinkList.removeIf(drink -> Objects.equals(drink.getId(), id));
    }
}
